package atguigu.reflect;

import cn.hutool.core.bean.BeanUtil;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ReflectClass {

    // ��������
    public static void reflectNewInstance() {
        try {
            Class<?> classBook = Class.forName("atguigu.reflect.Book");
            Object objectBook = classBook.newInstance();
            Book book = (Book) objectBook;
            book.setName("Android����֮��");
            book.setAuthor("������");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // ����˽�еĹ��췽��
    public static void reflectPrivateConstructor() {
        try {
            Class<?> classBook = Class.forName("atguigu.reflect.Book");
            Constructor<?> declaredConstructorBook = classBook.getDeclaredConstructor(String.class,String.class);
            declaredConstructorBook.setAccessible(true);
            Object objectBook = declaredConstructorBook.newInstance("Android��������̽��","�����");
            Book book = (Book) objectBook;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // ����˽������
    public static void reflectPrivateField() {
        try {
            Class<?> classBook = Class.forName("atguigu.reflect.Book");
            Object objectBook = classBook.newInstance();
            Field fieldTag = classBook.getDeclaredField("TAG");
            fieldTag.setAccessible(true);
            String tag = (String) fieldTag.get(objectBook);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // ����˽�з���
    public static void reflectPrivateMethod() {
        try {
            Class<?> classBook = Class.forName("atguigu.reflect.Book");
            Method methodBook = classBook.getDeclaredMethod("declaredMethod",int.class);
            methodBook.setAccessible(true);
            Object objectBook = classBook.newInstance();
            String string = (String) methodBook.invoke(objectBook,0);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * ��Object������������Ժ�ֵת����Map����
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<String,Object>();
        Class<?> clazz = obj.getClass();


        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
//            Object value = StringUtils.nvl(field.get(obj));
            Object value = field.get(obj);
            map.put(fieldName, value);
        }
        return map;
    }

    public static void main(String[] args) {
        Car car = new Car();
        car.setId("00000");
        car.setColor("black");
        car.setModel("bmw x5");
        car.setVolume("3.0L");
        car.setYear(2018);
        try {
            System.out.println(objectToMap(car));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(car);
        System.out.println(stringObjectMap);
    }
    
}